home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 …ember: Reference Library / Apple Developer Reference Library (December 1999) (Disk 1).iso / mac / Technical Documentation / Develop / develop Issue 24 / develop Issue 24 code / Scriptable Database 1.0a15.sea / Scriptable Database 1.0a15 / Foundation / EveryItemProxy.cp / EveryItemProxy.cp
Encoding:
Text File  |  1996-02-20  |  11.0 KB  |  282 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        EveryItemProxy.c
  3.  
  4.     Contains:    A proxy token for 'kAEAll' (every item of...)
  5.     
  6.                 Previously, 'kAEAll' just made a set of tokens
  7.                 for every item in the specified container.  This
  8.                 worked fine, but if an event did a get-data event
  9.                 on the resulting token without asking for a
  10.                 specific type, then the result would be a single
  11.                 reference if there was but one child, or a list
  12.                 if there were multiple items.  This was somewhat
  13.                 counter-intuitive, as it was expected that the
  14.                 result of 'every item of...' would always be a list,
  15.                 even if there was only a single object.
  16.                 
  17.                 Making a proxy for 'kAEAll' allowed this discrepency
  18.                 to be fixed, as it provids a way to specify that the
  19.                 default type for 'kAEAll' is typeList rather than
  20.                 typeObjectSpecifier.  It also avoids copious-property-token
  21.                 hell in cases such as "name of every item of..."
  22.  
  23.     Written by:    Greg Anderson
  24.  
  25.     Copyright:    © 1993-1995 by Apple Computer, Inc., all rights reserved.
  26.  
  27.          <8>     6/23/95    ga        
  28. */
  29.  
  30. #ifdef MWTRACEBACKTABLES
  31. #pragma traceback on
  32. #endif
  33.  
  34. #include "EveryItemProxy.h"
  35. #include "ScriptableObjectList.h"
  36.  
  37. #include <AERegistry.h>
  38. #include <AEObjects.h>
  39.  
  40. #include "Exceptions.h"
  41.  
  42. //========================================================================================
  43. // CLASS TEveryItemProxy
  44. //
  45. // This token represents every child of some other object
  46. //========================================================================================
  47.  
  48.  
  49. #pragma segment ObjectResident
  50. ImplementSmallClassData(TEveryItemProxy, clEveryItem);
  51.  
  52. #pragma segment Foundation
  53.  
  54. //----------------------------------------------------------------------------------------
  55. // TEveryItemProxy::TEveryItemProxy: 
  56. //----------------------------------------------------------------------------------------
  57. TEveryItemProxy::TEveryItemProxy()
  58.     {
  59.     fDesiredClass = typeWildCard;
  60.     } // TEveryItemProxy::TEveryItemProxy 
  61.  
  62. //----------------------------------------------------------------------------------------
  63. // TEveryItemProxy::~TEveryItemProxy: 
  64. //----------------------------------------------------------------------------------------
  65. TEveryItemProxy::~TEveryItemProxy()
  66.     {
  67.     } // TEveryItemProxy::~TEveryItemProxy 
  68.  
  69. //----------------------------------------------------------------------------------------
  70. // TEveryItemProxy::IEveryItemProxy: 
  71. //----------------------------------------------------------------------------------------
  72. void TEveryItemProxy::IEveryItemProxy(TAbstractScriptableObject* objectOfDesignation, DescType desiredClass)
  73.     {
  74.     fDesiredClass = desiredClass;
  75.     
  76.     Inherited::IProxyToken(objectOfDesignation);
  77.     } // TEveryItemProxy::IEveryItemProxy 
  78.  
  79. //----------------------------------------------------------------------------------------
  80. // TEveryItemProxy::DerivedFromOSLClass: 
  81. //----------------------------------------------------------------------------------------
  82. Boolean TEveryItemProxy::DerivedFromOSLClass(const TAETransaction& t, DescType objectClass)
  83.     {
  84.     return (objectClass == kAEAll) || (Inherited::DerivedFromOSLClass(t, objectClass));
  85.     } // TEveryItemProxy::DerivedFromOSLClass 
  86.  
  87. //----------------------------------------------------------------------------------------
  88. // TEveryItemProxy::DirectObjectIterator
  89. //----------------------------------------------------------------------------------------
  90. TAbstractObjectIterator* TEveryItemProxy::DirectObjectIterator(const TAETransaction& t)
  91.     {
  92.     TAbstractObjectIterator* delegateElements = this->ObjectOfDesignation()->ElementIterator(t);
  93.     
  94.     if(fDesiredClass == typeWildCard)
  95.         return delegateElements;
  96.     else
  97.         return new TEveryElementOfClassIterator(fDesiredClass, delegateElements);
  98.     }
  99.  
  100. //========================================================================================
  101. // Class TEveryElementOfClassIterator
  102. //========================================================================================
  103.  
  104. //----------------------------------------------------------------------------------------
  105. // TEveryElementOfClassIterator::~TEveryElementOfClassIterator
  106. //----------------------------------------------------------------------------------------
  107. TEveryElementOfClassIterator::~TEveryElementOfClassIterator()
  108. {
  109.     if(fIter)
  110.         fIter->Release();
  111. }
  112.  
  113. //----------------------------------------------------------------------------------------
  114. // TEveryElementOfClassIterator::Reset
  115. //----------------------------------------------------------------------------------------
  116. void TEveryElementOfClassIterator::Reset(const TAETransaction& t, Boolean iterationDirection /* = kForwardIteration */)
  117. {
  118.     //
  119.     // Reset to the beginning
  120.     //
  121.     if(fIter)
  122.     {
  123.         fIter->Reset(t, iterationDirection);
  124.         
  125.         //
  126.         // Then skip past items that are not derived from our restricted class
  127.         //
  128.         while(this->More(t) && (this->CurrentDerivedFromOSLClass(t, fDesiredClass) == false))
  129.             fIter->Next(t);
  130.     }
  131. }
  132.  
  133. //----------------------------------------------------------------------------------------
  134. // TEveryElementOfClassIterator::More
  135. //----------------------------------------------------------------------------------------
  136. Boolean TEveryElementOfClassIterator::More(const TAETransaction& t) const
  137. {
  138.     if(fIter)
  139.         return fIter->More(t);
  140.     else
  141.         return false;
  142. }
  143.  
  144. //----------------------------------------------------------------------------------------
  145. // TEveryElementOfClassIterator::Next
  146. //----------------------------------------------------------------------------------------
  147. void TEveryElementOfClassIterator::Next(const TAETransaction& t)
  148. {
  149.     if(fIter)
  150.     {
  151.         //
  152.         // Go to the next item in the list
  153.         //
  154.         fIter->Next(t);
  155.         
  156.         //
  157.         // Also skip past items that are not derived from our restricted class
  158.         //
  159.         while(this->More(t) && (this->CurrentDerivedFromOSLClass(t, fDesiredClass) == false))
  160.             fIter->Next(t);
  161.     }
  162. }
  163.  
  164. //----------------------------------------------------------------------------------------
  165. // TEveryElementOfClassIterator::Current
  166. //----------------------------------------------------------------------------------------
  167. TAbstractScriptableObject* TEveryElementOfClassIterator::Current(const TAETransaction& t)
  168. {
  169.     if(fIter)
  170.         return fIter->Current(t);
  171.     else
  172.         return nil;
  173. }
  174.  
  175. //----------------------------------------------------------------------------------------
  176. // TEveryElementOfClassIterator::CurrentDerivedFromOSLClass
  177. //----------------------------------------------------------------------------------------
  178. Boolean TEveryElementOfClassIterator::CurrentDerivedFromOSLClass(const TAETransaction& t, DescType objectClass)
  179. {
  180.     if(fIter)
  181.         return fIter->CurrentDerivedFromOSLClass(t, objectClass);
  182.     else
  183.         return false;
  184. }
  185.  
  186. //----------------------------------------------------------------------------------------
  187. // TEveryElementOfClassIterator::CountElements
  188. //----------------------------------------------------------------------------------------
  189. long TEveryElementOfClassIterator::CountElements(const TAETransaction& t, DescType desiredClass)
  190. {
  191.     DescType desiredClassToUse = this->DetermineAccessClass(desiredClass);
  192.     long count = 0;
  193.     
  194.     //
  195.     // If we can divine the correct desired class, then call our
  196.     // iterator to count the elements (may be an optimized version
  197.     // of 'CountElements').  If we cannot divine the correct desired
  198.     // class ("every alias file of every file of..."), then call
  199.     // Inherited::CountElements, which will always work but may be slower
  200.     // than calling the iterator directly.
  201.     //
  202.     if((fIter) && (desiredClassToUse != 0))
  203.         return fIter->CountElements(t, desiredClassToUse);
  204.     else
  205.         return TAbstractObjectIterator::CountElements(t, desiredClass);
  206. }
  207.  
  208. //----------------------------------------------------------------------------------------
  209. // TEveryElementOfClassIterator::GetIndexedElement
  210. //----------------------------------------------------------------------------------------
  211. TAbstractScriptableObject* TEveryElementOfClassIterator::GetIndexedElement(const TAETransaction& t, DescType desiredClass, long index)
  212. {
  213.     DescType desiredClassToUse = this->DetermineAccessClass(desiredClass);
  214.  
  215.     if((fIter) && (desiredClassToUse != 0))
  216.         return fIter->GetIndexedElement(t, desiredClassToUse, index);
  217.     else
  218.         return TAbstractObjectIterator::GetIndexedElement(t, desiredClass, index);    
  219. }
  220.  
  221. //----------------------------------------------------------------------------------------
  222. // TEveryElementOfClassIterator::GetNamedElement
  223. //----------------------------------------------------------------------------------------
  224. TAbstractScriptableObject* TEveryElementOfClassIterator::GetNamedElement(const TAETransaction& t, DescType desiredClass, TDescriptor nameDesc)
  225. {
  226.     DescType desiredClassToUse = this->DetermineAccessClass(desiredClass);
  227.  
  228.     if((fIter) && (desiredClassToUse != 0))
  229.         return fIter->GetNamedElement(t, desiredClassToUse, nameDesc);
  230.     else
  231.         return TAbstractObjectIterator::GetNamedElement(t, desiredClass, nameDesc);    
  232. }
  233.  
  234. //----------------------------------------------------------------------------------------
  235. // TEveryElementOfClassIterator::SearchDeep
  236. //----------------------------------------------------------------------------------------
  237. void TEveryElementOfClassIterator::SearchDeep(const TAETransaction& t, TAbstractCollector* collector, DescType desiredClass, TAbstractSearchSpec* searchSpec)
  238. {
  239.     this->RecursiveSearchDeep(t, collector, desiredClass, searchSpec);
  240. } // TEveryElementOfClassIterator::SearchDeep
  241.  
  242. //----------------------------------------------------------------------------------------
  243. // TEveryElementOfClassIterator::Contains
  244. //----------------------------------------------------------------------------------------
  245. Boolean TEveryElementOfClassIterator::Contains(const TAETransaction& t, TAbstractScriptableObject* objectToTestForMembership)
  246. {
  247.     if(fIter)
  248.         return fIter->Contains(t, objectToTestForMembership);
  249.     else
  250.         return false;
  251. }
  252.  
  253. //----------------------------------------------------------------------------------------
  254. // TEveryElementOfClassIterator::DetermineAccessClass: 
  255. //
  256. // This is used when someone says 'alias file 3 of every item of...'
  257. // In this example, 'alias file' is the desired class, and 'item' is
  258. // fDesiredClass.  This example should be the same as the statement
  259. // 'item 3 of every alias file of...', and 'alias file 3 of every alias file of...'.
  260. //
  261. // We currently are not smart enough to handle 'alias file 3 of every file of...',
  262. // because to do that we would have to know whether alias file or file is more
  263. // specific.  In that case we return '0' as the desired class to use, which
  264. // signals the iterator to fall back on slower iteration methods that handle the
  265. // more complex cases.
  266. //----------------------------------------------------------------------------------------
  267. DescType TEveryElementOfClassIterator::DetermineAccessClass(DescType desiredClass)
  268.     {
  269.     if((desiredClass == fDesiredClass) || (desiredClass == typeWildCard) || (desiredClass == cObject) || (desiredClass == cItem))
  270.         return fDesiredClass;
  271.     else if((fDesiredClass == typeWildCard) || (fDesiredClass == cObject) || (fDesiredClass == cItem))
  272.         return desiredClass;
  273.  
  274.     //
  275.     // We don't know which class is more specific (or if they intersect
  276.     // at all), so return 0 to signal this fact.
  277.     //
  278.     return 0;
  279.     } // TEveryElementOfClassIterator::DetermineAccessClass 
  280.  
  281. #pragma segment CFrontCruft
  282.